package org.mozhu.mboot.admin.system.resource.entity;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Formula;
import org.mozhu.mboot.core.data.entity.BaseTreeable;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Table;
import java.util.List;

/**
 * 资源 - 实体类
 *
 * @author ZhangPei
 */
@Entity
@Table(name = "sys_resource")
@Data
@EqualsAndHashCode(callSuper = true)
public class Resource extends BaseTreeable {

    private static final long serialVersionUID = -6872376783695447530L;

    /**
     * 英文名称
     */
    private String enName;

    /**
     * 描述
     */
    private String description;

    /**
     * 标识，全系统唯一
     */
    private String code;

    /**
     * 目前只有一种类型资源，默认为菜单
     */
    @Enumerated(EnumType.STRING)
    private ResourceType type = ResourceType.menu;

    /**
     * 链接
     */
    private String url;

    /**
     * 菜单显示所需权限 如system:resource，有多个，请以;隔开
     */
    private String permissions;


    public List<String> getPermissionCodes() {
        if (StringUtils.isEmpty(permissions)) {
            return Lists.newArrayList();
        }
        return Lists.newArrayList(Splitter.on(";").split(permissions));
    }

    /**
     * 是否有叶子节点
     */
    @Formula(value = "(select count(*) from sys_resource f_t where f_t.parent_id = id)")
    private boolean hasChildren;

}
